package dao.reader.impl;

import dao.AdminDao;
import dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * AdminDaoImpl
 *
 * @author ChenYanMei
 * @version 2023/8/29
 */
public class AdminDaoImpl extends BaseDao implements AdminDao {
    /**
     * 管理员登录
     *
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    @Override
    public String login(String userName, String password) {
        int row = 0;// 影响行数
        /**
         * 验证非空
         */
        if (userName.trim()==""){
            return "账号不能为空";
        }
        if (password.trim()==""){
            return "密码不能为空";
        }
        Connection conn = super.getConnection();// 获取连接
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select count(*) as c from admin where username=? and password=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, userName);
            pstmt.setString(2, password);

            rs = pstmt.executeQuery();
            if (rs.next()){
                row = rs.getInt("c");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            super.closeAll(rs, pstmt, conn);
        }
        return row==0?"账户或者密码错误":String.valueOf(row);
    }
}
